﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Controls;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
namespace WPFExcelReport.ViweModels
{
    public class ITXFOLDERSViewModel:BaseFunction
    {
        GB2312Convert8859P1 gb2312 = new GB2312Convert8859P1();
        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        private Dictionary<string, object> dicParamater
        {
            get;
            set;
        }
        public string Mandant
        {
            get
            {
                return app.Mandant;
            }
        }
        public string company
        {
            get
            {
                return app.company;
            }
        }
        public SETINTEXDB SetEnv
        {
            get;
            set;

        }
        public SETINTEXDB SetDetail
        {
            get;
            set;
        }
 
        public SETINTEXDB SetMaster
        {
            get;
            set;
        }
        INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        
        public ITXFOLDERSViewModel()
        {
            if (SetEnv == null)
            {
                SetEnv = new SETINTEXDB();
            }
            if (SetMaster == null)
            {
                SetMaster = new SETINTEXDB();
            }
            if (SetDetail == null)
            {
                SetDetail = new SETINTEXDB();
            }    
        }

        public DataSet LoadFolder(string client)
        { 
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                ArrayList tables = new ArrayList();
                string filePath = "";
                string fileroot = Path.Combine(PathHelper.ApplicationPath, @"Documents");
                string[] files = System.IO.Directory.GetFiles(fileroot);


                filePath = fileroot + "\\FOLDERS.XML";
                if (System.IO.File.Exists(filePath))
                {
                    setResult = new DataSet();
                    setResult.ReadXml(filePath);
                    if (setResult.Tables.Count > 0)
                    {
                        setResult.Tables[0].TableName = "ICC_FOLDERS";
                    }
                    setReturn.Merge(setResult);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public DataSet LoadITXFolder(string client)
        {
          
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataRow dwData = null;
            try
            {
                dwData = SetEnv.ICC_FOLDERS.NewRow();
                dwData["MANDANT"] = client;
                setResult = INTEXDBC.QueryICC_FOLDERS(dwData, 9999);
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public void SaveFolderSetup(DataSet src, string client)
        {
            try
            {
                /*
               if (src.Tables["ICC_FOLDERS"].Rows.Count > 0)
               {
                   INTEXDBC.DeleteICC_FOLDERS(client, "");
               }
               */
                foreach (DataRow rw in src.Tables["ICC_FOLDERS"].Rows)
                {
                    rw["MANDANT"] = client;

                    rw["ROOT"] = gb2312.ConvertBack(rw["ROOT"].ToString(), typeof(string), null, null);
                    rw["SUB"] = gb2312.ConvertBack(rw["SUB"].ToString(), typeof(string), null, null);
                    INTEXDBC.SaveICC_FOLDERS(rw);
                }

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}
